<?php
namespace app\admin\controller;

use app\plug\controller\qiniu\Qiniu;

use think\Db;
use think\Model;

/**
 * 商家管理
 */
class Shops extends Base
{
    /**
     * @throws
     * 商家列表
     * @return [type] [description]
     */
    public function index()
    {
        // 获取配置信息
        $conf = config();

        // 分页信息
        $pageSize   = input('limit')    ? input('limit')    : $conf['paginate']['list_rows'];

        // 查询数据
        $model = Model('shops');
        $list = $model->order('shop_id desc')->paginate($pageSize);

        $this->assign('total',$list->total());
        $this->assign('page',$list->render());
        $this->assign('list',$list);

        return $this->fetch();
    }


    /**
     * 添加商家
     */
    public function add()
    {
        $model = Model("shops");

        if(request()->isPost()){
            // 获取表单上传图片

            /*$file = request()->file('pic');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'shops');

                if($info){
                    $data['wx_logo']  = request()->domain().'/public/uploads/shops/'.$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }*/

            // 图片是否上传
            $file   = request()->file('pic');
            if($file){
                // 本地路径
                $filePath = $file->getRealPath();

                // 文件后缀
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

                // 七牛存储文件名
                $file_name = 'images/wx_logo/'.date("Ymd").rand(1000,9999).'.'.$ext;

                // 实例化七牛云
                $qiniu = new Qiniu();

                // 上传至七牛云
                $re = $qiniu->upload_pic($filePath,$file_name);
                if($re['code'] == 0){
                    $this->error("图片未成功上传七牛云".$re['data']);
                }

                $data['wx_logo']    = $re["data"];

            }

            // 商户信息
            $data['shop_no']        = input('shop_no');
            $data['user_name']      = input('user_name');
            $data['password']       = md5(trim(input('password')));
            $data['shop_name']      = input('shop_name');
            $data['can_open_store'] = input('can_open_store',1);
            $data['phone']          = input('phone');
            $data['shop_address']   = input('shop_address');
            $data['validity_time']  = input('validity_time',0) == 0 ? 0 : strtotime(input('validity_time'));
            $data['status']         = input('status',1);
            $data['link_name']      = input('link_name');
            $data['wx_name']        = input('wx_name');
            $data['wx_account']     = input('wx_account');
            $data['original']       = input('original');
            $data['appid']          = input('appid');
            $data['appsecret']      = input('appsecret');
            $data['token']          = get_rand_str(12);

            $shop_id = $model->insertGetId($data);
            if($shop_id){

                // 公众号开发服务器地址
                $model->where('shop_id',$shop_id)->update(['url'=>request()->domain().'/wchat/wchat/api?shop_id='.$shop_id]);

                // 记录日志
                $admin_log = new AdminLog();
                $admin_log->add('添加商家--'.$data['shop_name'],$data,'add');

                $this->success("添加成功");
            }else{
                $this->error("添加失败");
            }

        }

        // 商家最大编号
        $max_shop_id = $model->max('shop_id');
        $this->assign('shop_no',$max_shop_id+10001);

        // 店铺默认可开店铺数
        $can_open_store_num = Db("web_config")->where('key','max_store_num')->value('value');
        $this->assign('can_open_store_num',$can_open_store_num);

        // 一年后时间
        $this->assign('last_year',date("Y-m-d H:i:s",strtotime("+1 year")));

        return $this->fetch();
    }




    /**
     * @throws
     * 编辑商家
     */
    public function edit()
    {
        $shop_id = input('shop_id');

        // 实例化模型
        $model = model('shops');

        // 获取商户信息
        $shop_info = $model->where('shop_id',$shop_id)->find();

        $this->assign('info',$shop_info);

        // 更新数据
        if(request()->isPost()){

            $data = array();
            $re = '';

            /*// 图片更改
            $file = request()->file('pic');

            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'shops');

                if($info){
                    $data['wx_logo']  = request()->domain().'/public/uploads/shops/'.$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }*/

            // 图片是否被编辑
            $file   = request()->file('pic');
            if($file){
                // 本地路径
                $filePath = $file->getRealPath();

                // 文件后缀
                $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);

                // 七牛存储文件名
                $file_name = 'images/wx_logo/'.date("Ymd").rand(1000,9999).'.'.$ext;

                // 实例化七牛云
                $qiniu = new Qiniu();

                // 上传至七牛云
                $re = $qiniu->upload_pic($filePath,$file_name);
                if($re['code'] == 0){
                    $this->error("图片未成功上传七牛云".$re['data']);
                }

                $data['wx_logo']    = $re["data"];

            }

            // 商户信息
            input('shop_name')      == null || $data['shop_name']      = input('shop_name');
            input('link_name')      == null || $data['link_name']      = input('link_name');
            input('phone')          == null || $data['phone']          = input('phone');
            input('user_name')      == null || $data['user_name']      = input('user_name');
            input('password')       == null || $data['password']       = md5(trim(input('password')));
            input('wx_name')        == null || $data['wx_name']        = input('wx_name');
            input('wx_account')     == null || $data['wx_account']     = input('wx_account');
            input('original')       == null || $data['original']       = input('original');
            input('appid')          == null || $data['appid']          = input('appid');
            input('appsecret')      == null || $data['appsecret']      = input('appsecret');
            input('token')          == null || $data['token']          = input('token');
            input('shop_address')   == null || $data['shop_address']   = input('shop_address');
            input('validity_time')  == null || $data['validity_time']  = input('validity_time');
            input('status')         == null || $data['status']         = input('status',1);

            // 格式化时间转时间戳
            if(isset($data['validity_time']) && $data['validity_time'] != 0){
                $data['validity_time'] = strtotime($data['validity_time']);
            }

            if($data) {
                $re = $model->where('shop_id',$shop_id)->update($data);
            }

            if($re){

                // 记录日志
                $admin_log = new AdminLog();
                $admin_log->add('编辑商家--'.$shop_info['shop_name'],$data,'edit');

                $this->success('编辑成功',url('index'));
            }else{
                $this->error('未成功修改',url('edit',array('shop_id'=>$shop_id)));
            }

        }

        return $this->fetch();
    }


    /**
     * 处理店铺
     */
    public function deal()
    {
        // 参数验证
        $shop_id = input('shop_id');
        if($shop_id<=0){
            $this->error('缺少必要参数');
        }

        // 状态参数
        if(input('status') == null){
            $this->error('未作修改');
        }

        // 实例化shops模型
        $model = model('shops');

        // 更改数据库状态
        $re = $model->where('shop_id',$shop_id)->update(['status'=>input('status')]);
        if($re){

            // 插入日志
            $note = input('note') == null ? '操作店铺' : input('note');
            $admin_log = new AdminLog();
            $admin_log->add($note.$shop_id , input() , 'edit');

            $this->success();
        }else{
            $this->error("网络异常");
        }





    }
    
}
